var MiniMap = Class.extend({
	init: function(sprite, hFrames, vFrames){
		this.sprite = sprite;
		this.dx = hFrames;
		this.dy = vFrames;
		this.isShown = false;
	},
	draw: function(canvas, hero, map, quests){
		var x = 200;
		var y = 150;
		var w = 420;
		var h = 320;
		var mapFrame = 20;
		
		if (this.isShown){
			var horizontalUnit = (w / (canvas.width*this.dx));
			var verticalUnit = (h / (canvas.height*this.dy)) ;
			var heroX = hero.x * horizontalUnit;
			var heroY = hero.y * verticalUnit;
			canvas.ctx.save();
			canvas.ctx.fillStyle = '#b7a457';
			canvas.ctx.fillRect(x-mapFrame, y-mapFrame, w+mapFrame, h+mapFrame);
			canvas.ctx.drawImage(this.sprite.image, x, y);
			canvas.ctx.restore();
			if(!map.isChanging){
				canvas.ctx.save();
				canvas.ctx.fillStyle = 'white';
				canvas.ctx.translate((w - mapFrame)/this.dx*map.j,(h - mapFrame)/this.dy*map.i)
				canvas.ctx.fillRect(x + heroX, y + heroY, 10, 10);
				canvas.ctx.restore();
				
				for ( var i = 0; i < quests.length; i++) {
					var questX = (quests[i].x * horizontalUnit) + x + (w - mapFrame)/this.dx*quests[i].j;
					var questY= (quests[i].y * verticalUnit) + y + (h - mapFrame)/this.dy*quests[i].i;
					canvas.ctx.save();
					canvas.ctx.font = "18px arial";
					canvas.ctx.textAlign = 'center';
					canvas.ctx.fillStyle = '#ffffff';
					canvas.ctx.fillText(quests[i].token, questX, questY);
					canvas.ctx.restore();
				}
				
				
			}
			
			

		}
	
	},
	update: function(keysPress){
		if (109 in keysPress || 77 in keysPress){
			this.isShown = !this.isShown;
			delete keysPress[109];
			delete keysPress[77];
			
		}
	}
});

var Map = Class.extend({
	init: function(frames){
		this.isChanging = false;
		this.frames = [];
		//долните две оказват кой участък(кадър) от картата да се нарисува
		this.i = 0;
		this.j = 0;
		
		for(var i = 0; i < frames.length; i++){
			for(var j = 0; j < frames[i].length; j++){
				if(j==0){
					this.frames[i] = [];
				}
				this.frames[i][j] = frames[i][j];
			}
		}
	},
	draw: function (canvas, hero, bgObjects){

		if(this.isChanging){
			this.updateMapChange(canvas, hero, bgObjects);
			return null;
		}
		canvas.ctx.drawImage(this.frames[this.i][this.j].image,0,0,canvas.width,canvas.height);
		
		var fgObjects = {};
		var conter = 0;
		if (bgObjects){
			for (key in bgObjects[this.i][this.j]){
				if(!bgObjects[this.i][this.j][key].isOnForeground(hero)){
					bgObjects[this.i][this.j][key].draw(canvas);
				}else{
					fgObjects[conter++] = bgObjects[this.i][this.j][key];
					//delete bgObjects[i];
				}
			}
			return fgObjects
		}
		
	},
	updateMapChange: (function(){
			var steps = 40;
			var currentStep = 0;
			var i = 0;
			var j = 0;
			return function(canvas, hero, bgObjects){
				 
				if (++currentStep === steps){
					this.isChanging = false;
					currentStep = 0;
					this.j += j;
					this.i += i;
					this.draw(canvas, hero, bgObjects);
					return;
				}
				var direction = hero.direction;
				var pace; //броя пиксели с който се отмества новия и стария участък от картата за всеки frame;
				
				/*съдържа помощни коефициенти, с помощта на който промените на картата 
				се чертаят в правилната посока, определена от direction */
				var factor = [[0,-1],[-1,0],[0,1],[1,0]]; 
				
				//адреса на новия кадър(участък) от картата;
				j = factor[direction][0];
				i = factor[direction][1];
				
				switch (direction){
					case 0:
						pace = canvas.height/steps;
						break
					case 1:
						pace = canvas.width/steps;
						break;
					case 2:
						pace = canvas.height/steps;
						break;
					case 3:
						pace = canvas.width/steps;
						break;
				}
				var dFullx = steps * pace*factor[direction][0];
				var dFully = steps * pace*factor[direction][1];
				
				var dx = currentStep*pace*factor[direction][0];
				var dy = currentStep*pace*factor[direction][1];
				/*for (key in bgObjects[this.i][this.j]){
					bgObjects[this.i][this.j][key].x -= pace*factor[direction][0];
					bgObjects[this.i][this.j][key].y -= pace*factor[direction][1];
				}
				*/
				if(currentStep > 2){
					hero.x -= pace*factor[direction][0];
					hero.y -= pace*factor[direction][1];
					
				}
				canvas.ctx.drawImage(this.frames[this.i][this.j].image,(0 - currentStep*pace)*factor[direction][0], (0 - currentStep*pace)*factor[direction][1],canvas.width,canvas.height);
				for (key in bgObjects[this.i][this.j]){
					canvas.ctx.save();
					canvas.ctx.translate(-dx,-dy);
					bgObjects[this.i][this.j][key].draw(canvas);
					canvas.ctx.restore();
				}
				canvas.ctx.drawImage(this.frames[this.i + i][this.j + j].image,(canvas.width - currentStep*pace)*factor[direction][0], (canvas.height - currentStep*pace)*factor[direction][1],canvas.width,canvas.height);
				for (key in bgObjects[this.i + i][this.j + j]){
					canvas.ctx.save();
					canvas.ctx.translate(dFullx - dx, dFully - dy);
					bgObjects[this.i + i][this.j + j][key].draw(canvas);
					canvas.ctx.restore();
				}
			}
		})(),
		isClear: function(x, y, direction){
			switch(direction){
				case 0:			//up
					if (y <= 0){
						if (this.i === 0){
							return false;
						}
						this.isChanging = true;

					}
					break;
				case 1:
					if (x <= 0){
						if (this.j === 0){
							return false;
						}
						this.isChanging = true;
					}
					break;
				case 2:
					if (y > canvas.height-64){
						if (this.i === this.frames.length - 1){
							return false;
						}
						this.isChanging = true;
					}
					break;
				case 3:
					if (x >= canvas.width - 64){
						if (this.j === this.frames[0].length - 1){
							return false;
						}
						this.isChanging = true;
					}
					break;
				}    
			return true;
		}
});